package 力扣._230_二叉搜索树中第K小的元素;

import 力扣.公用数据结构.TreeNode;

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 * int val;
 * TreeNode left;
 * TreeNode right;
 * TreeNode() {}
 * TreeNode(int val) { this.val = val; }
 * TreeNode(int val, TreeNode left, TreeNode right) {
 * this.val = val;
 * this.left = left;
 * this.right = right;
 * }
 * }
 */
class Solution {
    // 中序遍历
    int now = 0;
    int k_small = -1;
    int k = -1;

    public void order(TreeNode root) {
        if (root == null || k_small != -1)
            return;

        order(root.left);
        now++;
        if (now == k)
            k_small = root.val;
        order(root.right);

    }

    public int kthSmallest(TreeNode root, int k) {
        this.k = k;
        order(root);
        return k_small;
    }
}